﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using Backend.JYD.Common;
using Backend.JYD.Messages.Exceptions;

namespace Backend.JYD.Config.Cache
{
    internal class CacheConfigurationSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (section == null)
                throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheSectionName, ConfigUtilities.ConfigName_CacheConfig);

            try
            {
                CacheConfig config = new CacheConfig();
                config.CacheType = NumberHelper.ObjectToInt(section.Attributes[ConfigUtilities.ConfigName_CacheConfig_CacheType].Value, -1);
                config.CacheTime = NumberHelper.ObjectToInt(section.Attributes[ConfigUtilities.ConfigName_CacheConfig_CacheTime].Value, 10);
                config.Perfix = section.Attributes[ConfigUtilities.ConfigName_CacheConfig_Perfix].Value;

                AppConfigs<CacheConfig>.Instance.AddConfig(config);
            }
            catch (Exception ex)
            {
                throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.TheSectionConfigError, ConfigUtilities.ConfigName_CacheConfig, ex.Message);
            }
            return AppConfigs<CacheConfig>.Instance;
        }
    }
}
